package principal;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ImportNote extends Import
{
	protected int colonne;
	protected int ligne;
	
	public String[][] getDonnees()
	{
		return this.donnees;
	}
	
	protected void definirTaille(String codeUE) throws SQLException, ClassNotFoundException, IndexOutOfBoundsException, BiffException, IOException
	{
		String[] num_et = new String[this.file.getNbLignes(1)];
		int nblignes = 0;
		for(int i = 18 ; i < this.file.getNbLignes(1) + 1 ; i++)
		{
			String l[] = this.file.lire(i, 1, 1);
	//		System.out.println(l[0]);
			if(l.length > 0)
			{
				if(!l[0].isEmpty())
				{
					num_et[i-18] = l[0].toLowerCase();
	//				System.out.println((i-18)+" : "+l[0]);
					nblignes++;
				}
			}
		}
		this.bd = Bd.getInstance();
		this.colonne = Integer.valueOf(this.bd.selectMaxNumColonne(codeUE))*2 +1;
		this.ligne = nblignes;
	//	System.out.println("this.colonne"+this.colonne+" this.ligne"+this.ligne);
		this.donnees = new String[this.ligne][this.colonne];
		for (int i = 0; i < this.donnees.length; i++)
		{
			this.donnees[i][0] = num_et[i];
		}
	}
	
	protected void envoyerDonnees(String codeUE) throws SQLException, ClassNotFoundException, IndexOutOfBoundsException, BiffException, IOException
	{
		this.bd = Bd.getInstance();
		this.definirTaille(codeUE);
		
		for(int i = 0 ; i < this.donnees.length ; i++)
		{
			for(int j = 0 ; j < (this.colonne / 2) ; j++)
			{
				String [] ligne = this.bd.selectNote(codeUE, Integer.toString(j+1), this.donnees[i][0]);
				this.donnees[i][j*2+1] = ligne[0];
				if(ligne[0].equals(""))
				{
					this.donnees[i][j*2+2] = "";
				}
				else
				{
					if(ligne[0].charAt(0) < '0' || ligne[0].charAt(0) > '9')
					{
						this.donnees[i][j*2+2] = "";
					}
					else
					{
						this.donnees[i][j*2+2] = ligne[1];
					}
				}
			}
		}
		this.bd.deconnect();
	}

	protected void extraireDonnees() throws SQLException, ClassNotFoundException, RowsExceededException, BiffException, WriteException, IllegalArgumentException, IOException
	{
		String[][] matrice = new String[this.ligne][this.colonne-1];
		for(int i = 0 ; i < this.ligne ; i++)
		{
			for (int j = 0; j < this.colonne-1; j++)
			{
				matrice[i][j] = this.donnees[i][j+1];
			}
		}
		this.file.ecrire(18, 5, matrice);
	}

	public void importer(String chemin, String codeUE) throws SQLException, ClassNotFoundException, RowsExceededException, BiffException, WriteException, IllegalArgumentException, IOException
	{
		this.file = new Jexcel(chemin);
		this.envoyerDonnees(codeUE);
		this.extraireDonnees();
	}

	protected boolean verifierFichier()
	{
		return false;
	}
	//fonction verifier notes de la classe ImportNote
	public String[][] selectionUesNotees() throws SQLException, ClassNotFoundException {

		  this.bd = Bd.getInstance();       
	         String strControle[][] = this.bd.selectControle();
	        String[][] strNote = null;
	        ArrayList<String> strArray = new ArrayList<String>();
	         
	          
	        boolean test = true;
	        for (int i = 0; i < strControle.length; i++)
	        {
	            strNote = this.bd.selectNote(strControle[i][5]);
	            test = (strNote.length != 0);
	           
	            for (int j = 0; j < strControle.length; j++) {
	                if (strControle[j][4].equals("0") && strControle[i][5].equals(strControle[j][5])){
	                    test = false;
	                }
	                if (j<i && strControle[i][5].equals(strControle[j][5])){    test = false;
	                }
	            }
	            for (int j = 0; j < strNote.length; j++)
	            {
	                if(strNote[j][1].equals("null")){test = false;}
	            }
	           
	            if (test)
	            {
	                if (strControle[i][4].equals("1"))
	                {
	                    strArray.add(strControle[i][5]+":"+bd.selectUE(strControle[i][5])[1]);
	                }
	            }
	        }
	     /*   for(int i = 0 ; i < strArray.size() ; i++)
	        {
	            System.out.println(strArray.get(i));
	        }*/
	       
	        String[][] noteRetour = new String[strArray.size()][2];
	        for (int i = 0; i < strArray.size() ; i++)
	        {
	            String[] tab = strArray.get(i).split(":");
	            if(tab.length == 0)
	            {
	                noteRetour[i][0] = "";
	                noteRetour[i][1] = "";
	            }
	            else if(tab.length == 1)
	            {
	                noteRetour[i][0] = tab[0];
	                noteRetour[i][1] = "";
	            }
	            else
	            {
	                noteRetour[i][0] = tab[0];
	                noteRetour[i][1] = tab[1];
	            }
	        }
	        this.bd.deconnect();
	        return noteRetour;

	       /*
	        String[][] maChaine = new String[2][2];
	        maChaine [0][0] = "UL3A04M";
	        maChaine [0][1] = "Gestion des processus";
	        return maChaine;*/
    }

}
